//
//  FamilyMedicalrecordViewController.m
//  安好
//
//  Created by lianchuang on 14-12-3.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "FamilyMedicalrecordViewController.h"
//病历模型
#import "FamilyModicalRecordModel.h"
//病历cell
#import "FamilyMedicalRecordCell.h"
//病历详情页面
//
#import "FamilyMedicalRecordDetialInfoViewController.h"

#define IPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)

@interface FamilyMedicalrecordViewController ()<UITableViewDelegate,UITableViewDataSource>

//tableView
@property(nonatomic,strong)UITableView * tableView;
/**
 *  保存一个一个模型，数组
 */
@property(nonatomic,strong)NSMutableArray * listDataAttay;

@end

@implementation FamilyMedicalrecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeNavBar];
    [self loadData];
    [self makeTableView];
    
}
#pragma mark - 导航条
-(void)makeNavBar
{
    self.navigationItem.title = self.navBarTitleWithFamilyName;
    // 返回按钮图片
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backBtnClicked)];
    backBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnImg;
    
    
}
-(void)backBtnClicked
{
    [self hiddenLabel];
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)loadData
{
    self.listDataAttay = [NSMutableArray arrayWithCapacity:0];
    [self AFNetRequestForFamilyMedicalRecordList];


}
#pragma mark - AFNet请求病历列表
-(void)AFNetRequestForFamilyMedicalRecordList
{
    [self showActionView];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString * getUrl = [NSString stringWithFormat:@"%@case/list/",kBaseURL];
    NSDictionary * parameters = @{@"platform":kPlatform,@"version":kVersion,@"fid":self.fid};
    
    [manager GET:getUrl parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        MyNSLog("成功");
        [self hiddenActionView];
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        if ([statusString isEqualToString:@"0"]) {

            [self shouLabel:@"暂无病历"];
        }
        else//成功
        {
            [self hiddenLabel];
            MyNSLog("%@",[responseObject objectForKey:@"msg"] );
            NSArray * dataArray = responseObject[@"data"];
            
            for (NSDictionary * dic in dataArray) {
                
                FamilyModicalRecordModel * modal = [[FamilyModicalRecordModel alloc] initWithDic:dic];
                [self.listDataAttay addObject:modal];
            }
            
            [self.tableView reloadData];
            
        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        MyNSLog("失败");
        [self hiddenLabel];
        if (self.listDataAttay.count == 0) {
            [self shouLabel:@"暂无病历"];
        }
        [self hiddenActionView];
    }];


}
#pragma mark - 创建tableView
-(void)makeTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-iOS7TopMargin) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //隐藏横向和竖向的滑条
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView setSeparatorColor:[UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1.0]];

}

#pragma mark - table的代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listDataAttay.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (IPhone6Plus) {
        return 145;
    }else{
        return 120;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
#pragma mark - cell---加载
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FamilyMedicalRecordCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (cell == nil) {
        if (IPhone6Plus) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"FamilyMedicalRecordCell" owner:self options:nil] lastObject];
        }else{
            cell = [[[NSBundle mainBundle] loadNibNamed:@"FamilyMedicalRecordCell" owner:self options:nil] firstObject];
        }
    }
    FamilyModicalRecordModel * modal = self.listDataAttay[indexPath.row];
    cell.titleLable.text = modal.title;
    cell.lastdiagnoseLable.text = modal.lastdiagnose;
    cell.dateLable.text = modal.date;
    [cell.DoctorAvatarImgView setImageWithURL:[NSURL URLWithString:modal.avatar] placeholderImage:[UIImage imageNamed:@"默认头像.png"]];
    cell.DoctorAvatarImgView.clipsToBounds = YES;
    cell.DoctorAvatarImgView.layer.cornerRadius = 3;
    
    if ([modal.firstflag intValue]!=0) {
        [cell.firstflagButton setTitle:@"初诊" forState:UIControlStateNormal];
    }else
    {
        [cell.firstflagButton setTitle:@"复诊" forState:UIControlStateNormal];
        //复诊的话把这个lable的背景变色
        cell.firstflagButton.backgroundColor = [UIColor grayColor];
    }
    if ([modal.picflag intValue] == 0) {
        cell.picflagButton.hidden = YES;
    }else
    {
        cell.picflagButton.hidden = NO;
    }

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FamilyMedicalRecordDetialInfoViewController * medicalDetial = [[FamilyMedicalRecordDetialInfoViewController alloc] init];
//    FamilyMedicalRecordDetialViewController * medicalDetial = [[FamilyMedicalRecordDetialViewController alloc] init];
    FamilyModicalRecordModel * modal = self.listDataAttay[indexPath.row];
    medicalDetial.userCaseModel = modal;
    [self.navigationController pushViewController:medicalDetial animated:YES];
    
}


@end

